package com.enbatis.mybatisplugs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.entity.User;
import com.enbatis.mybatisplugs.entity.vo.VegetableMenuVO;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.enbatis.mybatisplugs.toolkit.HttpRequestUtils;
import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.VegetableMenu;
import com.enbatis.mybatisplugs.service.VegetableMenuService;


import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("v1/vegetable_menu")
public class VegetableMenuController extends BaseController {

@Autowired
private VegetableMenuService vegetableMenuService;


    @PostMapping("/add")
    public ResultReturn<VegetableMenu> add(@RequestBody VegetableMenu vegetableMenu){
        return  success(vegetableMenuService.insert(vegetableMenu));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(vegetableMenuService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody VegetableMenu vegetableMenu){
        vegetableMenu.setId(id);
        return success(vegetableMenuService.updateById(vegetableMenu));
    }


    @GetMapping("/{id}")
    public ResultReturn<VegetableMenu> get(@PathVariable("id") Long id) {
        return success(vegetableMenuService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<VegetableMenu>> getList(VegetableMenu vegetableMenu){
        return success(vegetableMenuService.list(new Wrapper<>(vegetableMenu)));
    }



    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page")
    public ResultReturn<Pages<VegetableMenu>> getPage(@RequestBody VegetableMenu vegetableMenu){
        if (StringUtils.isBlank(vegetableMenu.getCpName())){
            return success(vegetableMenuService.page(getPage(),new Wrapper<>(vegetableMenu)));
        }else{

            Wrapper wrapper=  new Wrapper<>(new VegetableMenu()).like("cp_name",vegetableMenu.getCpName());
            List<VegetableMenu> list=vegetableMenuService.list(wrapper);
            if (CollectionUtils.isEmpty(list)){
                Map<String,String> map= Maps.newHashMap();
                map.put("key","ae2af80f2effd64987464199e04e5355");
                map.put("word",vegetableMenu.getCpName());
                JSONObject o= HttpRequestUtils.httpGet("http://api.tianapi.com/txapi/caipu/index", map);
                if (null!=o){
                   if ("200".equals(o.get("code").toString())){
                      JSONArray array= o.getJSONArray("newslist");
                      List<VegetableMenuVO> vegetableMenuVOList= array.toJavaList(VegetableMenuVO.class);
                      if (CollectionUtils.isNotEmpty(vegetableMenuVOList)){
                          for (VegetableMenuVO vo: vegetableMenuVOList) {
                              VegetableMenu vmenu = new VegetableMenu();
                              vmenu.setCpName(vo.getCp_name());
                              vmenu.setTypeId(vo.getType_id());
                              vmenu.setCpId(vo.getId());
                              vmenu.setTypeName(vo.getType_name());
                              vmenu.setTexing(vo.getTexing());
                              vmenu.setZuofa(vo.getZuofa());
                              vmenu.setTiaoliao(vo.getTiaoliao());
                              vmenu.setYuanliao(vo.getYuanliao());
                              vmenu.setTishi(vo.getTishi());
                              try {
                                  vegetableMenuService.insert(vmenu);
                              }catch (Exception e){
                                  e.printStackTrace();
                              }

                          }
                      }
                   }
                }
            }




            return success(vegetableMenuService.page(getPage(),wrapper));
        }



    }

}
